from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean
from sqlalchemy.orm import relationship
from .base import Base, TimestampMixin



__all__ = [
    'ProtectionRoom','SwitchgearCabinet','Lock', 'Device','CircuitBreaker','DeviceUnit','SoftPressurePlate','HardPressurePlate','SwitchSelection',
]


class ProtectionRoom(Base, TimestampMixin):
    """保护室数据模型"""
    __tablename__ = 'cy_protection_rooms'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='保护室唯一ID')
    name = Column(String(100), nullable=False, comment='保护室名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    substation_id = Column(Integer, ForeignKey('cy_substation.id'), nullable=False, comment='关联的变电站ID')
    substation = relationship("Substation")

    def __repr__(self):
        return f"<ProtectionRoom(id={self.id}, name='{self.name}', status={self.status})>"


class SwitchgearCabinet(Base, TimestampMixin):
    """屏柜数据模型"""
    __tablename__ = 'cy_switchgear_cabinets'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='屏柜唯一ID')
    name = Column(String(100), nullable=False, comment='屏柜名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    cabinet_type_id = Column(Integer, ForeignKey('cy_cabinet_types.id'), nullable=True, comment='屏柜类型ID')
    protection_room_id = Column(Integer, ForeignKey('cy_protection_rooms.id'), nullable=False, comment='关联的保护室ID')
    image_url = Column(String(255), nullable=False, comment='图片地址')

    def __repr__(self):
        return f"<SwitchgearCabinet(id={self.id}, name='{self.name}', type='{self.cabinet_type}')>"


class Lock(Base, TimestampMixin):
    """锁具数据模型"""
    __tablename__ = 'cy_protection_locks'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='锁具唯一ID')
    name = Column(String(100), nullable=False, comment='锁具名称')
    rfid = Column(String(100), nullable=False, comment='锁码')
    manufacturer = Column(String(100), nullable=False, comment='厂商')
    corresponding_area = Column(String(100), nullable=False, comment='所属区域')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False,
                                   comment='关联的屏柜ID')

    switchgear_cabinet = relationship('SwitchgearCabinet', backref='locks')

    def __repr__(self):
        return f"<Lock(id={self.id}, name='{self.name}')>"


class Device(Base, TimestampMixin):
    """硬压板数据模型"""
    __tablename__ = 'cy_protection_device'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')

    # 新增字段
    image_url = Column(String(255), nullable=True, comment='图片地址')
    row = Column(Integer, nullable=True, comment='硬压板的行号')
    column = Column(Integer, nullable=True, comment='硬压板的列号')
    device_type = Column(Integer, nullable=False, comment='设备类型')
    device_func_type = Column(Integer, nullable=True, comment='设备功能类型')
    device_id = Column(Integer, nullable=True, comment='关联的装置ID')

    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='protection_device')


    def __repr__(self):
        return f"<ProtectionDevice(id={self.id}, name='{self.name}', row={self.row}, column={self.column})>"


class CircuitBreaker(Base, TimestampMixin):
    """空开数据模型"""
    __tablename__ = 'cy_protection_circuit_breakers'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='空开名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')
    # 新增字段
    device_func_type = Column(Integer, nullable=True, comment='空开类型')
    image_url = Column(String(255), nullable=True, comment='图片地址')
    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='circuit_breakers')

    def __repr__(self):
        return f"<CircuitBreaker(id={self.id}, name='{self.name}', device_type={self.device_type})>"


class DeviceUnit(Base, TimestampMixin):
    """装置数据模型"""
    __tablename__ = 'cy_protection_device_units'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='装置名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')
    # 新增字段
    device_func_type = Column(Integer, nullable=True, comment='装置类型')
    image_url = Column(String(255), nullable=True, comment='图片地址')
    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='device_units')

    def __repr__(self):
        return f"<DeviceUnit(id={self.id}, name='{self.name}', device_type={self.device_type})>"

class SoftPressurePlate(Base, TimestampMixin):
    """软压板数据模型"""
    __tablename__ = 'cy_protection_soft_plates'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='软压板名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')

    # 新增字段
    image_url = Column(String(255), nullable=True, comment='图片地址')
    device_func_type = Column(Integer, nullable=True, comment='装置类型')
    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='soft_pressure_plates')

    def __repr__(self):
        return f"<SoftPressurePlate(id={self.id}, name='{self.name}', row={self.row}, column={self.column})>"

class HardPressurePlate(Base, TimestampMixin):
    """硬压板数据模型（已提供）"""
    __tablename__ = 'cy_protection_hard_plates'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='硬压板名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')

    # 新增字段
    image_url = Column(String(255), nullable=True, comment='图片地址')
    row = Column(Integer, nullable=True, comment='硬压板的行号')
    column = Column(Integer, nullable=True, comment='硬压板的列号')
    device_func_type = Column(Integer, nullable=True, comment='设备功能类型')
    device_id = Column(Integer, nullable=True, comment='关联的装置ID')

    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='hard_pressure_plates')


    def __repr__(self):
        return f"<HardPressurePlate(id={self.id}, name='{self.name}', row={self.row}, column={self.column})>"


class SwitchSelection(Base, TimestampMixin):
    """选开数据模型"""
    __tablename__ = 'cy_protection_switch_selections'

    id = Column(Integer, primary_key=True, autoincrement=True, comment='唯一ID')
    name = Column(String(100), nullable=False, comment='选开名称')
    status = Column(Boolean, default=True, comment='状态（True=启用，False=禁用）')
    switchgear_cabinet_id = Column(Integer, ForeignKey('cy_switchgear_cabinets.id'), nullable=False, comment='关联的屏柜ID')

    # 新增字段
    device_func_type = Column(Integer, nullable=True, comment='选开类型')
    image_url = Column(String(255), nullable=True, comment='图片地址')
    row = Column(Integer, nullable=True, comment='选开的行号')
    column = Column(Integer, nullable=True, comment='选开的列号')

    # 关系
    switchgear_cabinet = relationship('SwitchgearCabinet', backref='switch_selections')

    def __repr__(self):
        return f"<SwitchSelection(id={self.id}, name='{self.name}', device_type={self.device_type})>"


#
protection_room = ProtectionRoom.__table__
switchgear_cabinet = SwitchgearCabinet.__table__
protection_locks = Lock.__table__
protection_device = Device.__table__


protection_circuit_breakers = CircuitBreaker.__table__
protection_device_units = DeviceUnit.__table__
protection_soft_plates = SoftPressurePlate.__table__
protection_hard_plates = HardPressurePlate.__table__
protection_switch_selections = SwitchSelection.__table__



# from sqlalchemy import create_engine
#
# # 配置数据库连接（替换成你的实际数据库连接信息）
# engine = create_engine('mysql+pymysql://root:123456@localhost:3306/cygcadmin?charset=utf8mb4')
#
# # 使用 create_all() 创建所有未创建的表
# Base.metadata.create_all(engine)